package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.*;

//  权限列表业务层
@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Transactional
    @Override
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Transactional
    @Override
    public void deleteById(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission selectById(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.queryForList(qo);
        return new PageInfo<>(permissions);
    }

    // 把spring的上下文注入
    @Autowired
    private ApplicationContext ctx;

    // 加载权限
    @Override
    @Transactional
    public void reload() {
        // 获取所有的控制器(map)
        Map<String, Object> controllerMap = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> values = controllerMap.values();
        // 获取权限列表
        List<Permission> permissions = permissionMapper.selectAll();
        // 获取所有的权限表达式(用于去重,避免存重复的)
        List<String> expressions = new ArrayList<>();
        for (Permission permission : permissions) {
            expressions.add(permission.getExpression());
        }

        // 遍历控制器
        for (Object controller : values) {
            //贴了@RequiresPermissions的都会代理
            if (!AopUtils.isCglibProxy(controller)){
                continue;//不是代理对象跳过当前循环
            }
            // 获取每个控制器中对应的所有方法，通过的代理类的父类获取
            Method[] methods = controller.getClass().getSuperclass().getDeclaredMethods();
            // 遍历方法集
            for (Method method : methods) {
                // 获取方法上的RequiresPermissions注解(没贴的就是null)
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                // 判断方法上是否贴
                if (annotation != null){
                    // 有贴就获取该注解写的内容(value获取到的是数组,第一个写的表达式,第二个写的中文名)
                    String name = annotation.value()[1];
                    String expression = annotation.value()[0];
                    // 使用删除去重会对已经设置好的角色有影响,改为查询去重
                    // 如果该表达式不在数据库中就新建保存进去
                    if(!expressions.contains(expression)) {
                        // 封装成权限对象
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        // 把权限对象保存到数据库
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }
    // 通过员工id查询他的权限集合
    @Override
    public List<String> selectByEmpId(Long id) {
        return permissionMapper.selectByEmpId(id);
    }
}
